package model.handlers;

import database.Database;
import java.sql.SQLException;
import model.BSSingleton;
import model.User;

/**
 * 
 * @author Sonny Sandberg
 */
public abstract class UserHandler 
{
    public static boolean login(String username, String password) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        String[][] result = db.getFromDB("bs_user", "WHERE username = '"+username+"' AND password = '"+password+"'", "", "", "id", "username", "password", "email", "securitykey", "access_level");
        db.closeDB();
        
        if (result.length == 1)
        {
            User user = new User(Integer.parseInt(result[0][0]), result[0][1], result[0][2], result[0][3], result[0][4], Integer.parseInt(result[0][5]));
            BSSingleton.getSingletonObject().setLoggedInUser(user);
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public static boolean logout()
    {
        BSSingleton.getSingletonObject().clearLoggedInUser();
        return true;
    }
    
    public static User getUserByID(int id) throws ClassNotFoundException, SQLException
    {
        User user = null;
        
        Database db = new Database();
        String[][] result = db.getFromDB("bs_user", "WHERE id = "+id+"", "", "", "id", "username", "password", "email", "securitykey", "access_level");
        db.closeDB();
        
        if (result.length > 0)
        {
            user = new User(Integer.parseInt(result[0][0]), result[0][1], result[0][2], result[0][3], result[0][4], Integer.parseInt(result[0][5]));
        }
        
        return user;
    }
    
    public static User[] getAllUsers(String where, String order, String limit) throws ClassNotFoundException, SQLException
    {
        User[] users = null;
        
        Database db = new Database();
        String[][] result = db.getFromDB("bs_user", where, order, limit, "id", "username", "password", "email", "securitykey", "access_level");
        db.closeDB();
        
        users = new User[result.length];
        for (int i = 0; i < result.length; i++)
        {
            users[i] = new User(Integer.parseInt(result[i][0]), result[i][1], result[i][2], result[i][3], result[i][4], Integer.parseInt(result[i][5]));
        }
        
        return users;
    }
    
    public static boolean isUsernameAvailable(User user) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        String[][] result = db.getFromDB("bs_user", "WHERE username = '"+user.getUsername()+"'", "", "", "id");
        db.closeDB();
        
        if (result.length == 0 && user.getUsername().length() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public static boolean isEmailAvailable(User user) throws ClassNotFoundException, SQLException
    {
        Database db = new Database();
        String[][] result = db.getFromDB("bs_user", "WHERE email = '"+user.getEmail()+"'", "", "", "id");
        db.closeDB();
        
        if (result.length == 0 && user.getEmail().length() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}